<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head>
    <title>Client1主页</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <!--/* Jquery */-->
    <script th:src="@{/webAjax/webjars/jquery/3.6.0/jquery.min.js}"></script>
</head>
<body>
    <div th:text="${welcomeMsg}"></div>
    <ul>
        <li><a th:href="@{/webAjax/temp}">跳转其他页面</a><br/></li>
        <li><a th:href="@{/webAjax/articles}">向Web Ajax Server发送请求1 - 查询文章列表</a><br/></li>
        <li><a onclick="submitAjax('https://scg-client-prod:8080/user_info', 'GET')">[Ajax]获取当前SCG已登录的用户信息</a><br/></li>
        <li><a onclick="submitAjax('/webAjax/articles', 'GET')">[Ajax]向Web Ajax Server发送请求1 - 查询文章列表</a><br/></li>
        <li><a onclick="submitAjax('/webAjax/articles', 'POST')">[Ajax]向Web Ajax Server发送请求2 - 查询文章列表</a><br/></li>
        <li><a th:href="@{https://scg-client-prod:8080/logout}">SCG登出</a></li>
    </ul>

<script type="text/javascript">
    $.ajaxSetup({
        type: "GET",
        dataType: "jsonp",
        error: function(xhr, exception){
            alert("请求发送失败 - " + xhr.status);
            if( xhr.status == "401") {
                const homepage = "https://scg-client-prod:8080/webAjax";
                alert('认证失败 - 重定向到homepage: ' + homepage + '\n通过任意html页面请求触发自动登录');
                window.location.href = homepage;
            }
        }
    });

    /**
     * 提交异步Ajax请求
     */
    function submitAjax(requestUrl, method) {
        //提交登录请求
        $.ajax({
            url:  requestUrl,
            method: method,
            contentType: 'application/json',
            dataType: "json"
            })
            .done(function(respResult) {
                alert("URL: " + requestUrl + "\nmethod: " + method + "\n请求结果：" + JSON.stringify(respResult))
            })
            // .fail(function () {
            //     //登录请求发送失败
            //     alert("请求发送失败！");
            // })
            ;
    }

</script>

</body>
</html>